package com.common.utils;

import java.util.Arrays;
import java.util.Optional;


public class EnumHelper {
    /**
     * 根据code 获取枚举类对象
     *
     * @param code
     * @param <T>
     * @return
     */
    public static <T extends IBaseEnum> T fromCode(Integer code, Class<T> clazz) {
        if (null != code) {
            Optional<T> optional = Arrays.asList(clazz.getEnumConstants()).stream().filter(x -> x.getCode() - code == 0).findAny();
            if (optional.isPresent()) {
                return optional.isPresent() ? optional.get() : null;
            }
        }
        return null;
    }
}
